//
//  DiscoverSerachView.swift
//  WeiBo14
//
//  Created by rzj on 16/3/9.
//  Copyright © 2016年 rzj. All rights reserved.
//

import UIKit

class DiscoverSerachView: UIView {
    
    @IBOutlet weak var canleBtn: UIButton!
 
    @IBOutlet weak var inputField: UITextField!

    //文本框右边约束
    @IBOutlet weak var fieldConstraintsRight: NSLayoutConstraint!
    
    //文本框开始编辑
    @IBAction func fieldEditingDidBeginOnlick(sender: AnyObject) {
        
        print("文本框开始编辑")
        
        //显示按钮 约束放在动画中无效!!! 使用动画用layoutIfNeeded
        self.fieldConstraintsRight.constant = self.canleBtn.frame.width

        UIView.animateWithDuration(0.25) { () -> Void in
            //如果需要 自动布局
            self.inputField.layoutIfNeeded()
        }
    }
   
    @IBAction func btnOnlickCanle(sender: UIButton) {
        print("取消")
        
        //恢复显示
        fieldConstraintsRight.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.inputField.layoutIfNeeded()
        }
        
        //关闭键盘
        inputField.resignFirstResponder()
    }
    
    //init?(coder aDecoder: NSCoder) 控件还未连上线 未初始化完毕 不能使用
    override func awakeFromNib() {
    
        inputField.leftView = leftView
        //设置总是显示 默认不显示
        inputField.leftViewMode = .Always
        //设置leftView的大小为文本框高度大小
        inputField.leftView?.frame.size = CGSize(width: self.frame.height, height: self.frame.height)
        
        //设置边框
        inputField.layer.cornerRadius = 5
        inputField.layer.borderWidth = 1
        inputField.layer.borderColor = UIColor.orangeColor().CGColor
        inputField.layer.masksToBounds = true
        
    }
    
    
    //MARK: - 懒加载leftView
    private lazy var leftView:UIView = {
        
        let imgView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置内容居中
        imgView.contentMode = .Center
        return imgView
    }()
    
    //类方法以class开头
    class func serachView() -> DiscoverSerachView  {
    
        //返回值为AnyObject 要强转as!为DiscoverSerachView
        return NSBundle.mainBundle().loadNibNamed("DiscoverSerachView", owner: nil, options: nil).last as! DiscoverSerachView
    
    }
    
    
    
}
